package com.ruoyi.framework.listener;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.framework.config.model.SysDict;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.impl.SysConfigServiceImpl;
import com.ruoyi.system.service.impl.SysDictDataServiceImpl;
import com.ruoyi.system.service.impl.SysDictTypeServiceImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

//系统默认配置
@Configuration
@Order(Ordered.LOWEST_PRECEDENCE)
public class SystemParamInit implements ApplicationListener<ApplicationStartedEvent> {

    private static final Logger log = LoggerFactory.getLogger(SystemParamInit.class);

    private List<SysConfig> configList;

    private List<SysDict> dictList;

    @Autowired
    private SysConfigServiceImpl configService;

    @Autowired
    private SysDictTypeServiceImpl dictTypeService;

    @Autowired
    private SysDictDataServiceImpl dictDataService;

    void systemConfigInit() {
        log.info("初始化系统配置...");
        if (configList != null) {
            for (SysConfig sysConfig : configList) {
                if (UserConstants.CONFIG_KEY_UNIQUE.equals(configService.checkConfigKeyUnique(sysConfig))) {
                    configService.insertConfig(sysConfig);
                    log.info("初始化系统配置" + sysConfig + "完毕");
                } else {
                    log.info("跳过系统配置" + sysConfig);
                }
            }
        }
        log.info("初始化系统配置结束");
        log.info("初始化系统字典...");
        if (dictList != null) {
            for (SysDict dict : dictList) {
                if (UserConstants.CONFIG_KEY_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
                    dictTypeService.insertDictType(dict);
                    log.info("初始化系统字典:" + dict);
                    for (SysDictData dictData : dict.getListDatas()) {
                        dictData.setDictType(dict.getDictType());
                        if (UserConstants.CONFIG_KEY_UNIQUE.equals(dictDataService.checkDictDataUnique(dictData))) {
                            dictDataService.insertDictData(dictData);
                            log.info("初始化系统字典:" + dictData);
                        } else {
                            log.info("跳过系统字典:" + dictData);
                        }
                    }
                } else {
                    log.info("系统字典跳过:" + dict);
                }
            }
        }
        log.info("初始化系统字典结束");
    }

    /**
     * @return the configList
     */
    public List<SysConfig> getConfigList() {
        return configList;
    }

    /**
     * @param configList the configList to set
     */
    public void setConfigList(List<SysConfig> configList) {
        this.configList = configList;
    }

    /**
     * @return the dictList
     */
    public List<SysDict> getDictList() {
        return dictList;
    }

    /**
     * @param dictList the dictList to set
     */
    public void setDictList(List<SysDict> dictList) {
        this.dictList = dictList;
    }

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        loadYmlByResources();
        systemConfigInit();

    }

    void loadYmlByResources() {

        YamlMapFactoryBean yamMap = new YamlMapFactoryBean();
        ClassPathResource reourcesPath = new ClassPathResource("/data/data.yml");
        yamMap.setResources(reourcesPath);
        Object obConfigs = yamMap.getObject().get("configList");
        List<SysConfig> listConfigs = JSON.parseArray(JSON.toJSONString(obConfigs), SysConfig.class);
        setConfigList(listConfigs);
        Object obDicts = yamMap.getObject().get("dictList");
        List<SysDict> listDict = JSON.parseArray(JSON.toJSONString(obDicts), SysDict.class);
        setDictList(listDict);
    }

    void loadYmlByFile() {
        YamlMapFactoryBean yamMap = new YamlMapFactoryBean();
        FileSystemResource fileResource = new FileSystemResource("");
        yamMap.setResources(fileResource);
        Object obConfigs = yamMap.getObject().get("configList");
        List<SysConfig> listConfigs = JSON.parseArray(JSON.toJSONString(obConfigs), SysConfig.class);
        setConfigList(listConfigs);

    }

}
